// pages/home/repair/repair-handle/repairHandle.js
import {
    getRequest
} from '../../../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        repairList: [],
        repairingList:[],
        repairedList:[],
        active: 0
    },
    onChange(event) {
        this.setData({
            active: event.detail.index
        })
        const {
            userId
        } = wx.getStorageSync('user')
        if(event.detail.index===0){
            this.getRepairList(userId)
        }else if(event.detail.index===1){
            this.getRepairingList(userId)
        }else{
            this.getRepairedList(userId)
        }
    },
    contact(e) {
        let phoneNumber = e.currentTarget.dataset.phone
        wx.makePhoneCall({
            phoneNumber: phoneNumber, //仅为示例，并非真实的电话号码
            success() {
                console.log('接口调用成功的回调函数')
            },
            fail() {
                console.log('接口调用失败的回调函数')
            },
            complete() {
                console.log('接口调用结束的回调函数（调用成功、失败都会执行）')
            }
        })
    },
    handleDetail(e){
        let repairId = e.currentTarget.dataset.id
        if (repairId) {
            wx.navigateTo({
                url: `./handle/handle?repairId=${repairId}`,
            })
        }
    },
    handle(e) {

        let repairId = e.currentTarget.dataset.id
        console.log(repairId);
        if (repairId) {
            // wx.navigateTo({
            //     url: `./handle/handle?repairId=${repairId}`,
            // })
           
            this.handleStatus(repairId)
        }
    },
    async handleStatus(repairId) {
        wx.showLoading({
            title: '开始处理',
        })
        const {
            data
        } = await getRequest(`/base/repair/handle?repairId=${repairId}`)
        if (data == 1) {
            wx.hideLoading()
            this.setData({
                active: 1
            })
            const {
                userId
            } = wx.getStorageSync('user')
            this.getRepairingList(userId)
            this.getRepairList(userId)
        }
    },
    async getRepairList(userId) {
        const {
            data
        } = await getRequest(`/base/repair/getRepairListByUserId?userId=${userId}&status=1`, "")
        console.log(data);
        this.setData({
            repairList: data
        })
    },
    async getRepairingList(userId) {
        const {
            data
        } = await getRequest(`/base/repair/getRepairListByUserId?userId=${userId}&status=2`, "")
        console.log(data);
        this.setData({
            repairingList: data
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const {
            userId
        } = wx.getStorageSync('user')
        this.getRepairList(userId)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})